/*
 * jngc, Java utility classes by Nicholas Coltharp
 * Copyright (C) 2010 Nicholas Coltharp <ngc1@rice.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.rice.ngc1.util;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * This class serves the role of a dynamic enum. It enumerates a fixed
 * set of values provided at runtime. Methods and attributes inherited
 * from {@code WrappingCounter} allow for the concept of a
 * "selection", a single value in the list which is considered
 * "selected." When the user increments the count, the selection
 * becomes the next item in the list, until the end is reached and
 * count resets to 0.
 * 
 * @param <T>
 *            the type that this list enumerates
 */
public class SelectionList<T> extends WrappingCounter {

    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = -5748579639630109272L;

    /**
     * The internal list of items. Immutable.
     */
    private final List<T> items;

    /**
     * Constructs a new {@code SelectionList} from the given items and
     * a current value of 0.
     * 
     * @param items
     *            a list containing the items to enumerate
     */
    public SelectionList(List<T> items) {
        this(items, 0);
    }

    /**
     * Constructs a new {@code SelectionList} from the given items
     * items and with the given value.
     * 
     * @param items
     *            the items to enumerate; must not be empty
     * @param value
     *            the value to start at; must be at least 0 and less
     *            than {@code items.size()}
     */
    public SelectionList(List<T> items, int value) {
        super(items.size(), value);
        List<T> tmpItems = new LinkedList<T>(items);
        this.items = Collections.unmodifiableList(tmpItems);
    }

    /**
     * Gets the currently selected item.
     * 
     * @return the selection
     */
    public T getSelection() {
        return this.items.get(this.getValue());
    }

    @Override
    public boolean equals(Object obj) {
        if (!super.equals(obj)) {
            return false;
        }
        SelectionList<?> other = (SelectionList<?>) obj;
        return this.items.equals(other.items);
    }

    @Override
    public int hashCode() {
        int hash = HashCodeGenerator.getInitialValue();
        hash = HashCodeGenerator.hash(hash, super.hashCode());
        hash = HashCodeGenerator.hash(hash, this.items);
        return hash;
    }

    @Override
    public SelectionList<T> clone() {
        return new SelectionList<T>(items, getValue());
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("[");
        Strings.join(items.subList(0, getValue()), ", ", builder);
        builder.append("<");
        builder.append(getSelection());
        builder.append(">");
        if (getValue() < items.size() - 1) {
            builder.append(", ");
            Strings.join(items.subList(getValue() + 1, items.size()), ", ");
        }
        builder.append("]");
        return builder.toString();
    }
}
